import router from "@/router";
import axios from 'axios'

export default class OrderValidator {
  async sign ({ orderId }, $vue) {
    const signUrl = `/order/sign`;
    let response = await axios.put(signUrl, { orderId, signType: 0 });
    if (0 == response.code) {
      $vue.$message({ type: 'success', message: response.message });
      $vue.search();
    }
  }

  validate ({ type, orderId, status, exceptions, routerName, selection, trackType, transportStatus }, $vue) {
    switch (type) {
      case 'sign':
        if ([1, 3].includes(trackType) && [1, 2].includes(transportStatus)) {
          return $vue.$confirm("订单没到目的地，确认签收吗？", "提示", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            this.sign({ orderId }, $vue);
            return Promise.reject(new Error("订单没到目的地"))
          });
        }
        break;

      case 'receipt':
        let flag = false; //签收异常
        exceptions && exceptions.map(item => {
          if (item === 1) {
            flag = true;
          }
        })
        if (flag) {
          return $vue.$confirm("订单有异常，是否进入详情页确认？", "提示", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            router.push({
              name: routerName,
              query: {
                orderId: orderId
              }
            });
          });
        }
        break;

      case 'batchReceipt':
        if (selection?.length === 0) {
          $vue.$message({ type: 'warning', message: "请选择至少一条记录" })
          return Promise.reject(new Error("请选择至少一条记录"))
        }

      // let invalidArr = [];
      // selection.map(item => {
      //   if (item.exceptions && item.exceptions.length) {
      //     item.exceptions.map(ele => {
      //       if (ele === 1) {
      //         invalidArr.push(item.contractNo);
      //       }
      //     })
      //   }
      // })
      // if (invalidArr.length === selection.length) {
      //   return $vue.$confirm(`所选订单全部有异常，不能进行批量回单。`,
      //     "提示",
      //     {
      //       confirmButtonText: "我知道了",
      //       type: "warning",
      //       showClose: false,
      //       showCancelButton: false,
      //       closeOnClickModal: false
      //     }).then(() => {
      //       return Promise.reject(new Error("所选订单全部有异常，不能进行批量回单。"))
      //     });
      // }
      // if (invalidArr.length) {
      //   let invalid = invalidArr.map(item => `"${item}"`);
      //   let tips = invalid.toString().replace(new RegExp(',', 'g'), '，');
      //   return $vue.$confirm(`订单中有异常，异常订单：${tips}，可提交正常订单，是否执行？`,
      //     "提示",
      //     {
      //       confirmButtonText: "是",
      //       cancelButtonText: "否",
      //       type: "warning",
      //       closeOnClickModal: false
      //     }).then(() => {
      //       return Promise.resolve(true)
      //     });
      // }
      // break;
    }
  }
}